<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Linux 命令- cp | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Linux 命令- cp</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cp命令用来复制文件或者目录，是Linux系统中最常用的命令之一。一般情况下，shell会设置一个别名，在命令行下复制文件时，如果目标文件已经存在，就会询问是否覆盖，不管你是否使用-i参数。但是如果是在shell脚本中执行cp时，没有-i参数时不会询问是否覆盖。这说明命令行和shell脚本的执行方式有些不同。 </p>
<h2 id="1．命令格式："><a href="#1．命令格式：" class="headerlink" title="1．命令格式："></a>1．命令格式：</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>用法：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp [选项] [-T] [源] [目的]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp [选项] [源] [目录]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp [选项] [-t] [目录]  [源]</div></pre></td></tr></table></figure>
<h2 id="2．命令功能："><a href="#2．命令功能：" class="headerlink" title="2．命令功能："></a>2．命令功能：</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将源文件复制至目标文件，或将多个源文件复制至目标目录。</p>
<h2 id="3．命令参数："><a href="#3．命令参数：" class="headerlink" title="3．命令参数："></a>3．命令参数：</h2><ul>
<li>-a, –archive    等于-dR –preserve=all    <ul>
<li>–backup[=CONTROL  为每个已存在的目标文件创建备份</li>
</ul>
</li>
<li>-b  类似–backup 但不接受参数     <ul>
<li>–copy-contents   在递归处理是复制特殊文件内容</li>
</ul>
</li>
<li>-d  等于–no-dereference –preserve=links</li>
<li>-f, –force 如果目标文件无法打开则将其移除并重试(当 -n 选项  存在时则不需再选此项)</li>
<li>-i, –interactive   覆盖前询问(使前面的 -n 选项失效)</li>
<li>-H   跟随源文件中的命令行符号链接</li>
<li>-l –link 链接文件而不复制</li>
<li>-L –dereference   总是跟随符号链接</li>
<li>-n –no-clobber   不要覆盖已存在的文件(使前面的 -i 选项失效)</li>
<li>-P –no-dereference   不跟随源文件中的符号链接</li>
<li>-p   等于–preserve=模式,所有权,时间戳    <ul>
<li>–preserve[=属性列表   保持指定的属性(默认：模式,所有权,时间戳)，如果可能保持附加属性：环境、链接、xattr 等</li>
</ul>
</li>
<li>-R, -r, –recursive  复制目录及目录内的所有项目</li>
</ul>
<h2 id="4．命令实例："><a href="#4．命令实例：" class="headerlink" title="4．命令实例："></a>4．命令实例：</h2><h3 id="实例1：复制单个文件到目标目录，文件在目标文件中不存在"><a href="#实例1：复制单个文件到目标目录，文件在目标文件中不存在" class="headerlink" title="实例1：复制单个文件到目标目录，文件在目标文件中不存在"></a>实例1：复制单个文件到目标目录，文件在目标文件中不存在</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp log.log test5</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cp log.log test5</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root    0 10-28 14:48 log.log</div><div class="line">drwxr-xr-x 6 root root 4096 10-27 01:58 scf</div><div class="line">drwxrwxrwx 2 root root 4096 10-28 14:47 test3</div><div class="line">drwxr-xr-x 2 root root 4096 10-28 14:53 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test5</span></div><div class="line">[root@localhost test5]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:53 log.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在没有带-a参数时，两个文件的时间是不一样的。在带了-a参数时，两个文件的时间是一致的。</p>
<h3 id="实例2：目标文件存在时，会询问是否覆盖"><a href="#实例2：目标文件存在时，会询问是否覆盖" class="headerlink" title="实例2：目标文件存在时，会询问是否覆盖"></a>实例2：目标文件存在时，会询问是否覆盖</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp log.log test5</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cp log.log test5</span></div><div class="line">cp：是否覆盖“test5/log.log”? n</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cp -a log.log test5</span></div><div class="line">cp：是否覆盖“test5/log.log”? y</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test5/</span></div><div class="line">[root@localhost test5]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:48 log.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;目标文件存在时，会询问是否覆盖。这是因为cp是cp -i的别名。目标文件存在时，即使加了-f标志，也还会询问是否覆盖。 </p>
<h3 id="实例3：复制整个目录"><a href="#实例3：复制整个目录" class="headerlink" title="实例3：复制整个目录"></a>实例3：复制整个目录</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp -a test3 test5</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;目标目录存在时：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cp -a test3 test5 </span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root    0 10-28 14:48 log.log</div><div class="line">drwxr-xr-x 6 root root 4096 10-27 01:58 scf</div><div class="line">drwxrwxrwx 2 root root 4096 10-28 14:47 test3</div><div class="line">drwxr-xr-x 3 root root 4096 10-28 15:11 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test5/</span></div><div class="line">[root@localhost test5]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root    0 10-28 14:46 log5-1.log</div><div class="line">-rw-r--r-- 1 root root    0 10-28 14:46 log5-2.log</div><div class="line">-rw-r--r-- 1 root root    0 10-28 14:46 log5-3.log</div><div class="line">-rw-r--r-- 1 root root    0 10-28 14:48 log.log</div><div class="line">drwxrwxrwx 2 root root 4096 10-28 14:47 test3</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;目标目录不存在是：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cp -a test3 test4</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root    0 10-28 14:48 log.log</div><div class="line">drwxr-xr-x 6 root root 4096 10-27 01:58 scf</div><div class="line">drwxrwxrwx 2 root root 4096 10-28 14:47 test3</div><div class="line">drwxrwxrwx 2 root root 4096 10-28 14:47 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-28 15:11 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意目标目录存在与否结果是不一样的。目标目录存在时，整个源目录被复制到目标目录里面    </p>
<h3 id="实例4：复制的-log-log-建立一个连结档-log-link-log"><a href="#实例4：复制的-log-log-建立一个连结档-log-link-log" class="headerlink" title="实例4：复制的 log.log 建立一个连结档 log_link.log"></a>实例4：复制的 log.log 建立一个连结档 log_link.log</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp -s log.log log_link.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cp -s log.log log_link.log</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">lrwxrwxrwx 1 root root    7 10-28 15:18 log_link.log -&gt; log.log</div><div class="line">-rw-r--r-- 1 root root    0 10-28 14:48 log.log</div><div class="line">drwxr-xr-x 6 root root 4096 10-27 01:58 scf</div><div class="line">drwxrwxrwx 2 root root 4096 10-28 14:47 test3</div><div class="line">drwxrwxrwx 2 root root 4096 10-28 14:47 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-28 15:11 test5</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;那个 log_link.log 是由 -s 的参数造成的，建立的是一个『快捷方式』，所以您会看到在文件的最右边，会显示这个文件是『连结』到哪里去的！</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/2. Linux 命令/9. Linux 命令- cp/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
